package cn.admin.configcenter.configservice.service.event;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CopyOnWriteArrayList;

public class EventDispatcher {

    private static final Logger LOG =
            LoggerFactory.getLogger(EventDispatcher.class);
    private static final CopyOnWriteArrayList<EventListener> listeners =
            new CopyOnWriteArrayList<>();

    public static void addEventListener(EventListener listener) {
        listeners.add(listener);
    }

    public static void fireEvent(Event event) {

        if (event == null) {
            LOG.error("event cannot be null");
            throw new IllegalArgumentException();
        } else {
            for (EventListener listener : listeners) {
                listener.onEvent(event);
            }
        }

    }

}
